# vim: ai ts=2 sts=2 et sw=2

import os

import utils
import map
import unit
import item

class World:
  def __init__(self):
    self.map = map.Map()
    self.unitList = []
    self.itemList = []
    self.jobList = []

  def generate(self):
    # current world generation is loading from a handcraft xml file
    self.map.loadMapXML(os.path.join("data", "field.xml"))

    self.map.create(None)
    
    # add some units
    self.unitList.append(unit.Unit("human-adult-male-front.png"))
    self.unitList.append(unit.Unit("dwarf-adult-male-front.png"))

    mushroom = item.Item("purple_mushroom.png")
    mushroom.x = 20
    mushroom.y = 10
    self.itemList.append(mushroom)
    bush = item.Item("bush.png")
    bush.x = 2
    bush.y = 2
    self.itemList.append(bush)

  def update(self):
    i = 0
    while i < len(self.jobList):
      for currentUnit in self.unitList:
        if currentUnit.job is None:
          currentUnit.job = self.jobList[i]
          del self.jobList[i]
          i -= 1
          break
      i += 1
      
    for currentUnit in self.unitList:
      currentUnit.update(self)
